﻿using System;
using System.ComponentModel;
using System.Web.Mvc;
using FsCarInfo.Core.Entity;
using FsCarInfo.Facade;
using HMIS.FrameWork;
using HMIS.Infrastructure.Core.Authority;
using HMIS.Infrastructure.Facade.Authority;
using HMIS.MVC.CommonSupport;
using HMIS.MVC.ViewModel;
using Iesi.Collections.Generic;

namespace HMIS.MVC.Controllers
{
	/// <summary>
	/// 公务用车管理页面
	/// </summary>
	public class BCarController : BaseController
	{
		private readonly IBCarFacade BCarFacade;
		private readonly IHospUserFacade HospUserFacade;
		//private readonly IActionPermissionFacade ActionPermissionFacade;

		public BCarController(IBCarFacade BCarFacade, IHospUserFacade HospUserFacade)
		{
			this.BCarFacade = BCarFacade;
			this.HospUserFacade = HospUserFacade;
			//this.ActionPermissionFacade = ActionPermissionFacade;
		}

		[Description("获取公务用车清单")]
		public ActionResult GetPlistAll()
		{
			long total;
			var data = BCarFacade.GetPlistAll(
				int.Parse(Request["start"] == null ? "0" : Request["start"]),
				int.Parse(Request["limit"] == null ? "0" : Request["limit"]),
				Request["sort"],
				Request["dir"],
				this.DesDataFilter(Request["filter"]),
				out total);
			return this.JsonFormat(
				new ExtGirdData<BCar> { total = total, data = data },
				new string[] { "CustomType" });
		}

		[Description("访问公务用车管理页面")]
		[ViewPage]
		public ActionResult BCar()
		{
			return View();
		}

		[ExtResult]
		[Description("添加修改公务用车信息")]
		public ActionResult SaveOrUpdate()
		{
			BCar entity;
			if (String.IsNullOrEmpty(Request["Id"]))
			{
				entity = new BCar
				{
					Type = Request["Type"],
					CarNo = Request["CarNo"],
					CarOwner = Request["CarOwner"],
					DeptID = Request["DeptID"],
					Deptartment = Request["Deptartment"],
					Description = Request["Description"],
					AuthorizeUsers = new HashedSet<HospUser>()
				};
			}
			else
			{
				entity = BCarFacade.GetById(Request["Id"]);
				entity.Type = Request["Type"];
				entity.CarNo = Request["CarNo"];
				entity.CarOwner = Request["CarOwner"];
				entity.DeptID = Request["DeptID"];
				entity.Deptartment = Request["Deptartment"];
				entity.AuthorizeUsers.Clear();
			}
			// 授权用户
			string[] authusers = Request["AuthorizeUsers"].Split(',');
			for (int i = 0; i < authusers.Length; i++)
			{
				entity.AuthorizeUsers.Add(HospUserFacade.Load(authusers[i]));
			}
			// 保存信息
			BCarFacade.SaveOrUpdate(entity);

			return this.JsonFormat(new ExtResult { success = true, id = entity.Id });
		}

		[Description("获取公务用车信息")]
		public ActionResult GetById()
		{
			var entity = BCarFacade.GetById(Request["Id"]);
			return this.JsonFormat(
			   new ExtFormLoadData<BCar> { data = new BCar[] { entity } }, new string[] { "AuthorizeUsers" }
			   );
		}

		[ExtResult]
		[Description("判断车辆车牌号是否唯一")]
		public ActionResult IsFieldExist()
		{
			var result = BCarFacade.IsFieldExist(Request["FieldName"], Request[Request["FieldName"]], Request["Id"]);
			return this.JsonFormat(new ExtResult { success = !result, msg = "已存在相同的项!" });
		}

		[ExtResult]
		[Description("删除公务用车基本资料")]
		public ActionResult Delete()
		{
			BCarFacade.Delete(Request["Id"]);
			return this.JsonFormat((new ExtResult { success = true }));
		}

	}
}
